package com.bdmc.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.base.domain.BaseFencePoint;
import com.bdmc.base.service.IBaseFencePointService;
import com.bdmc.common.annotation.DataColumn;
import com.bdmc.common.annotation.DataPermission;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.base.domain.bo.BaseFenceBo;
import com.bdmc.base.domain.vo.BaseFenceVo;
import com.bdmc.base.domain.BaseFence;
import com.bdmc.base.mapper.BaseFenceMapper;
import com.bdmc.base.service.IBaseFenceService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 网格Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-03
 */
@Service
public class BaseFenceServiceImpl extends ServicePlusImpl<BaseFenceMapper, BaseFence, BaseFenceVo> implements IBaseFenceService {

    @Autowired
    IBaseFencePointService fencePointService;

    @Override
    public BaseFenceVo queryById(Long fenceId){
        return getVoById(fenceId);
    }

    @Override
    public TableDataInfo<BaseFenceVo> queryPageList(BaseFenceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseFence> lqw = buildQueryWrapper(bo);
        Page<BaseFenceVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<BaseFenceVo> mySelectPage(BaseFenceBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<BaseFenceVo> queryList(BaseFenceBo bo) {
        return  baseMapper.mySelectPage(bo);
    }

    private LambdaQueryWrapper<BaseFence> buildQueryWrapper(BaseFenceBo bo) {
        LambdaQueryWrapper<BaseFence> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, BaseFence::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getFenceName()), BaseFence::getFenceName, bo.getFenceName());
        lqw.eq(StringUtils.isNotBlank(bo.getFenceType()), BaseFence::getFenceType, bo.getFenceType());
        return lqw;
    }

    @Transactional
    @Override
    public Boolean insertByBo(BaseFenceBo bo) {
        BaseFence add = BeanUtil.toBean(bo, BaseFence.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            if(bo.getFencePointList()!=null && bo.getFencePointList().size()>0){
                bo.getFencePointList().forEach(r->r.setFenceId(add.getFenceId()));
                fencePointService.saveBatch(BeanUtil.copyToList(bo.getFencePointList(), BaseFencePoint.class));
            }
            bo.setFenceId(add.getFenceId());
        }
        return flag;
    }

    @Transactional
    @Override
    public Boolean updateByBo(BaseFenceBo bo) {
        BaseFence update = BeanUtil.toBean(bo, BaseFence.class);
        validEntityBeforeSave(update);
        boolean result = updateById(update);
        if (result) {
            if(bo.getFencePointList()!=null && bo.getFencePointList().size()>0){
                fencePointService.remove(new LambdaQueryWrapper<BaseFencePoint>().eq(BaseFencePoint::getFenceId,bo.getFenceId()));
                bo.getFencePointList().forEach(r->r.setFenceId(bo.getFenceId()));
                fencePointService.saveBatch(BeanUtil.copyToList(bo.getFencePointList(), BaseFencePoint.class));
            }
        }
        return result;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BaseFence entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        boolean result = removeByIds(ids);
        LambdaQueryWrapper<BaseFencePoint> w1 = new LambdaQueryWrapper<>();
        w1.in(BaseFencePoint::getFenceId,ids);
        if(result){
            fencePointService.remove(w1);
        }
        return result;
    }
}
